package service

import (
	"singo/model"
	"singo/serializer"

	"github.com/jinzhu/gorm"
)

// ManageMemberService 项目管理列表服务
type ManageMemberService struct {
	Limit     int    `form:"pageSize" json:"pageSize"`
	Start     int    `form:"pageNo" json:"pageNo"`
	StartTime string `form:"start_time" json:"start_time"`
	EndTime   string `form:"end_time" json:"end_time"`
}

func SearchWithDeadlineAt(StartTime string, EndTime string) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("deadline_at BETWEEN ? AND ?", StartTime, EndTime)
	}
}

func SearchWithCreatedAt(StartTime string, EndTime string) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("created_at BETWEEN ? AND ?", StartTime, EndTime)
	}
}

func SearchWithExecuteBy(UserUID string) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("execute_by LIKE ?", "%"+UserUID+"%")
	}
}

// ManageMemberList 项目管理列表
func (service *ManageMemberService) ManageMemberList() serializer.Response {
	manageMembers := []model.ManageMember{}
	users := []model.User{}
	total := 0
	task_total := 0
	task_done := 0
	weight_total := 0.0
	weight_done := 0.0
	demand_total := 0
	demand_done := 0
	defect_total := 0
	defect_done := 0
	overdue_total := 0
	check_total := 0
	check_done := 0

	if service.Limit == 0 {
		service.Limit = 10
	}

	if err := model.DB.Model(&users).Count(&total).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Find(&users).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	for _, item := range users {
		if service.StartTime != "" && service.EndTime != "" {
			// 任务
			if err := model.DB.Model(&model.Task{}).Scopes(SearchWithDeadlineAt(service.StartTime, service.EndTime), SearchWithExecuteBy(item.UID)).Count(&task_total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			if err := model.DB.Model(&model.Task{}).Scopes(SearchWithDeadlineAt(service.StartTime, service.EndTime), SearchWithExecuteBy(item.UID)).Where("status = ?", 3).Count(&task_done).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			// 任务系数
			tasks := []model.Task{}
			// tasks2 := []model.Task{}
			weight_total = 0.0
			weight_done = 0.0

			if err := model.DB.Scopes(SearchWithDeadlineAt(service.StartTime, service.EndTime), SearchWithExecuteBy(item.UID)).Not("status in (?)", []int{3, 4}).Find(&tasks).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			for _, task := range tasks {
				weight_total += task.Weight
			}
			// if err := model.DB.Scopes(SearchWithDeadlineAt(service.StartTime, service.EndTime), SearchWithExecuteBy(item.UID)).Where("status = ?", 3).Find(&tasks2).Error; err != nil {
			// 	return serializer.Response{
			// 		Status: 50000,
			// 		Msg:    "数据库连接错误",
			// 		Error:  err.Error(),
			// 	}
			// }
			// for _, task := range tasks2 {
			// 	weight_done += task.Weight
			// }
			// 需求
			if err := model.DB.Model(&model.Demand{}).Scopes(SearchWithCreatedAt(service.StartTime, service.EndTime), SearchWithExecuteBy(item.UID)).Count(&demand_total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			if err := model.DB.Model(&model.Demand{}).Scopes(SearchWithCreatedAt(service.StartTime, service.EndTime), SearchWithExecuteBy(item.UID)).Where("status = ?", 3).Count(&demand_done).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			// 缺陷
			if err := model.DB.Model(&model.Defect{}).Scopes(SearchWithCreatedAt(service.StartTime, service.EndTime), SearchWithExecuteBy(item.UID)).Count(&defect_total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			if err := model.DB.Model(&model.Defect{}).Scopes(SearchWithCreatedAt(service.StartTime, service.EndTime), SearchWithExecuteBy(item.UID)).Where("status = ?", 3).Count(&defect_done).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			// 核查
			if err := model.DB.Model(&model.Task{}).Scopes(SearchWithDeadlineAt(service.StartTime, service.EndTime), SearchWithExecuteBy(item.UID)).Where("status = ?", 3).Count(&check_total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			if err := model.DB.Model(&model.Task{}).Scopes(SearchWithDeadlineAt(service.StartTime, service.EndTime), SearchWithExecuteBy(item.UID)).Where("review_status = ?", 1).Where("status = ?", 3).Count(&check_done).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
		} else {
			// 任务
			if err := model.DB.Model(&model.Task{}).Scopes(SearchWithExecuteBy(item.UID)).Count(&task_total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			if err := model.DB.Model(&model.Task{}).Scopes(SearchWithExecuteBy(item.UID)).Where("status = ?", 3).Count(&task_done).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			// 任务系数
			tasks := []model.Task{}
			// tasks2 := []model.Task{}
			weight_total = 0.0
			weight_done = 0.0

			if err := model.DB.Scopes(SearchWithExecuteBy(item.UID)).Not("status in (?)", []int{3, 4}).Find(&tasks).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			for _, task := range tasks {
				weight_total += task.Weight
			}
			// if err := model.DB.Scopes(SearchWithExecuteBy(item.UID)).Where("status = ?", 3).Find(&tasks2).Error; err != nil {
			// 	return serializer.Response{
			// 		Status: 50000,
			// 		Msg:    "数据库连接错误",
			// 		Error:  err.Error(),
			// 	}
			// }
			// for _, task := range tasks2 {
			// 	weight_done += task.Weight
			// }
			// 需求
			if err := model.DB.Model(&model.Demand{}).Scopes(SearchWithExecuteBy(item.UID)).Count(&demand_total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			if err := model.DB.Model(&model.Demand{}).Scopes(SearchWithExecuteBy(item.UID)).Where("status = ?", 3).Count(&demand_done).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			// 缺陷
			if err := model.DB.Model(&model.Defect{}).Scopes(SearchWithExecuteBy(item.UID)).Count(&defect_total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			if err := model.DB.Model(&model.Defect{}).Scopes(SearchWithExecuteBy(item.UID)).Where("status = ?", 3).Count(&defect_done).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			// 核查
			if err := model.DB.Model(&model.Task{}).Scopes(SearchWithExecuteBy(item.UID)).Where("status = ?", 3).Count(&check_total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			if err := model.DB.Model(&model.Task{}).Scopes(SearchWithExecuteBy(item.UID)).Where("review_status = ?", 1).Where("status = ?", 3).Count(&check_done).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
		}

		manageMember := model.ManageMember{
			Nickname: item.Nickname,
			Task: model.TotalInterface{
				Total: task_total,
				Done:  task_done,
			},
			Weight: model.TotalInterface2{
				Total: weight_total,
				Done:  weight_done,
			},
			Demand: model.TotalInterface{
				Total: demand_total,
				Done:  demand_done,
			},
			Defect: model.TotalInterface{
				Total: defect_total,
				Done:  defect_done,
			},
			Overdue: overdue_total,
			Check: model.TotalInterface{
				Total: check_total,
				Done:  check_done,
			},
		}
		manageMembers = append(manageMembers, manageMember)
	}

	return serializer.BuildListResponse(serializer.BuildMembersManage(manageMembers), uint(total))
}
